#!/usr/bin/ruby
require 'webrick'

include WEBrick                 # let's import the namespace so I don't have to keep typing  WEBrick:
require "config.rb"             #our configuration file


#loading servlet

require "servlets/ping.rb"      #handle ping request
require "servlets/status.rb"    #handle status request
require "servlets/submit.rb"

#loading our class

require "class/client.class.rb" #handle clients
require "class/pass.class.rb"   #handle hash
require "class/job.class.rb"    #handle job
require "class/split.class.rb"  #handle job split

#loading function

require "function.rb"           #useful function

#loadi dynamic page content

require "dynamic-pages/headers.rb"
require "dynamic-pages/footer.rb"


#configuring ngale server
config = {}
config.update(:Port => $port)


#global structures

$clients      = {} #clients list
$clientNum    = 0 #number of clients


$numHash     =  0 #number of hash to crack
$numPass     =  0 #number of hash cracked
$hashBySec   =  0 #hash by sec 
$keyspace    =  0 #keyspace we are exploring

$jobs        = {} #jobs to do
$jobDone     = 0  #number of jobs done
$jobNum      = 1  #number of jobs

$splits      = [] #current active splits
$splitNum    = 0


#starting the web-server
server = HTTPServer.new(config)

#loading our servelets

server.mount('/', StatusServlet)
server.mount('/status', StatusServlet)
server.mount('/submit', SubmitServlet)
#client api servelet
server.mount('/ping', PingServlet)

#static page
server.mount("/static", HTTPServlet::FileHandler, $staticRoot,  {:FancyIndexing=>true})







#dealing with signal
yield server if block_given? 
['INT', 'TERM'].each {|signal| 
    trap(signal) {server.shutdown}
}

$start = Time.new()
server.start
